package com.zues.samples.web;

import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zues.samples.model.User;
import com.zues.samples.service.UserService;
import com.zues.samples.service.modelVO.UserVO;

@Controller
public class LoginController {

	@Autowired
	private UserService userService;

	@RequestMapping(value = "login", method = RequestMethod.GET)
	public String toLogin(Model model) {
		model.addAttribute("userVO", new UserVO());
		return "common/login";
	}

	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String doLogin(UserVO userVO, RedirectAttributes redirectAttributes, HttpSession session) {
		if (userVO != null) {
			try {
				User tempUser = userService.getUserById(userVO.getUserID());
				if (tempUser != null) {
					Calendar currentCalendarTime = Calendar.getInstance();
					currentCalendarTime.setTime(new Date());
					currentCalendarTime.add(Calendar.MINUTE, -5);
					Date currentTime = currentCalendarTime.getTime();

					if (tempUser.getLastLockTime().before(currentTime)) {
						// 不在锁定时间内

						// MD5
						// String encodePassword = MD5Util.GetMD5Code(userVO.getPassword());
						String encodePassword = userVO.getPassword();
						if (encodePassword.equals(tempUser.getPassword())) {
							// 登陆成功 失败次数清零
							if(tempUser.getLoginFailCount()>0){
								tempUser.setLoginFailCount(0);
								userService.updateUserLoginFailCount(tempUser);;
							}
							session.setAttribute("LoginUser", tempUser);
							return "redirect:/index";
						} else {
							// 密码错误
							tempUser.setLoginFailCount(tempUser.getLoginFailCount() + 1);
							if (tempUser.getLoginFailCount() == 3) {
								tempUser.setLoginFailCount(0);
								tempUser.setLastLockTime(new Date());
								userService.updateUserLastLockTime(tempUser);
							}
							userService.updateUserLoginFailCount(tempUser);;
							redirectAttributes.addAttribute("ErrMsg", "用户名或密码错误");
							return "redirect:/login";
						}
					} else {
						// 再锁定时间范围内
						redirectAttributes.addAttribute("ErrMsg", "账户锁定中,请稍后再试");
						return "redirect:/login";
					}
				} else {
					// 用户不存在
					redirectAttributes.addAttribute("ErrMsg", "用户不存在");
					return "redirect:/login";
				}
			} catch (Exception e) {
				e.printStackTrace();
				return "redirect:/login";
			}
		} else {
			// 没输入
			redirectAttributes.addAttribute("ErrMsg", "没输入账号密码");
			return "redirect:/login";
		}
	}

	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String toIndex(Model model) {
		model.addAttribute("userVO", new UserVO());
		return "common/index";
	}
}
